#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QPalette>
#include <QChartView>
#include <QDebug>
#include <QString>
#include <QValueAxis>
QString qlabel_open =  "background-image: url(:/iconJinggao.png);"
                       "background-repeat: no-repeat;"
                       "padding-left: 50px;"
                       "background-position: left center;"
                       "width: 312px;"
                       "height: 73px;"
                       "color: #000000;"  // 字体颜色设置
        "font-size: 30px;"
        "font-weight: bold;"  // 使用 bold 来替代 700
        "font-family: 'HarmonyOS Sans SC';"
        "line-height: 73px;";  // 确保与组件高度一致

QString qlabel_close =  "background-image: url(:/iconJinggao.png);"
                        "background-repeat: no-repeat;"
                        "padding-left: 50px;"
                        "background-position: left center;"
                        "width: 312px;"
                        "height: 73px;"
                        "color: rgb(228, 228, 228);"  // 字体颜色设置
        "font-size: 30px;"
        "font-weight: bold;"  // 使用 bold 来替代 700
        "font-family: 'HarmonyOS Sans SC';"
        "line-height: 73px;";  // 确保与组件高度一致


QString actualQLabel_open =
        "width: 418px;"                 // 宽度
        "height: 73px;"                 // 高度
        "color: rgb(7, 105, 217);"      // 文本颜色
        "font-size: 25px;"              // 字体大小
        "font-weight: bold;"            // 字体加粗
        "font-family: 'HarmonyOS Sans SC';";  // 字体类型

QString actualQLabel_close =
        "width: 418px;"                 // 宽度
        "height: 73px;"                 // 高度
        "color: rgb(228, 228, 228);"      // 文本颜色
        "font-size: 25px;"              // 字体大小
        "font-weight: bold;"            // 字体加粗
        "font-family: 'HarmonyOS Sans SC';";  // 字体类型


mainWidget::mainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::mainWidget)
{
    ui->setupUi(this);
    QPixmap pixmap(":/LOGO.png");
    ui->TopLabel->setFixedHeight(60);
    // 计算缩放后的宽高（80%）
    int scaledWidth = static_cast<int>(pixmap.width() * 0.9);
    int scaledHeight = static_cast<int>(pixmap.height() * 0.9);
    // 缩放图片到80%
    QPixmap scaledPixmap = pixmap.scaled(scaledWidth, scaledHeight,
                                         Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->TopLabel->setPixmap(scaledPixmap);
    ui->weight_Btn->setCheckable(true);
    ui->RFID_Btn->setCheckable(true);

    // 创建曲线图
    QVBoxLayout* layout = new QVBoxLayout;
    chart = new QChart();
    for (int i = 0; i < 4; i++) {
        QLineSeries* series = new QLineSeries();  // 创建一个新的 QLineSeries 实例
        series->setName("weight" + QString::number(i+1));
        series_vec.append(series);               // 将实例添加到 QVector 中
        chart->addSeries(series);                // 将 series 添加到 chart
    }
    //    series = new QLineSeries();
    //    chart->addSeries(series);
    // 创建坐标轴
    QValueAxis* axisX = new QValueAxis();
    axisX->setRange(0, 100); // 设置 X 轴的范围为 0 到 100
    QValueAxis* axisY = new QValueAxis();
    axisY->setRange(0, 1500); // 设置 Y 轴的范围为 0 到 100
    //    chart->setTitle("WEIGHT Chart");
    // 将坐标轴添加到图表
    chart->addAxis(axisX, Qt::AlignBottom); // 将 X 轴放置在底部
    chart->addAxis(axisY, Qt::AlignLeft);   // 将 Y 轴放置在左侧
    for (int i = 0; i < 4; i++) {
        chart->series().at(i)->attachAxis(axisX);
        chart->series().at(i)->attachAxis(axisY);
    }
    chartView = new QChartView(chart);
    chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
    chartView->chart()->legend()->show();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(chartView);
    ui->weight_View->setLayout(layout);
    ui->weight_View->update(); // 刷新视图

    rfidThread = new RFIDThread;
    weightThread = new WEIGHTThread;

    connect(weightThread, &WEIGHTThread::curNum, this, [=](int bar, QString time, int weight)
    {
        QLineSeries* series = series_vec.at(bar-1);
        ui->actualWeightTime_Label->setText(time);
        ui->actualInstantWeight_Label->setText( QString::number(weight) + " 克 ");
        ui->actualWeightBar_Label->setText(QString::number(bar) + " 号通道");

        // 添加数据到曲线
        int count = series->count();
        series->append(count, weight); // X轴为数据点的数量，Y轴为weight
        if (count >= axisX->max()) {
            // 扩展 X 轴范围到当前最大值的 2 倍
            axisX->setRange(0, axisX->max() * 2);
        }
        chart->update(); // 更新图表
    });

    connect(rfidThread, &RFIDThread::curNum, this, [=](int bar, QString time, QString epc)
    {
        ui->actualEPCTime_Label->setText(time);
        ui->actualInstantEPC_Label->setText(epc);
        ui->actualRFIDNum_Label->setText(QString::number(bar) + " 号天线 ");
    });
}

mainWidget::~mainWidget()
{
    delete ui;
}

void mainWidget::on_weight_Btn_toggled(bool checked)
{
    if(checked) {
        ui->weight_Btn->setText("关闭读取重量");
        weightThread->start();
        ui->weightBar_Label->setStyleSheet(qlabel_open);
        ui->weightTime_Label->setStyleSheet(qlabel_open);
        ui->instantWeight_Label->setStyleSheet(qlabel_open);
        ui->actualWeightBar_Label->setStyleSheet(actualQLabel_open);
        ui->actualInstantWeight_Label->setStyleSheet(actualQLabel_open);
        ui->actualWeightTime_Label->setStyleSheet(actualQLabel_open);

    } else {
        weightThread->stop();
        for(int i = 0; i < 4; i++)
            series_vec.at(i)->clear(); // 清除series中的所有数据
        //        series->clear();
        //        chart->axisX()->setRange(0, 100); // 根据需要设置X轴范围
        //        chart->axisY()->setRange(0, 100); // 根据需要设置Y轴范围
        chart->axisX()->setRange(0, 100);
        ui->weight_Btn->setText(" 开始读取重量 ");
        ui->weightBar_Label->setStyleSheet(qlabel_close);
        ui->weightTime_Label->setStyleSheet(qlabel_close);
        ui->instantWeight_Label->setStyleSheet(qlabel_close);
        ui->actualWeightBar_Label->setStyleSheet(actualQLabel_close);
        ui->actualInstantWeight_Label->setStyleSheet(actualQLabel_close);
        ui->actualWeightTime_Label->setStyleSheet(actualQLabel_close);
    }
}

void mainWidget::on_RFID_Btn_toggled(bool checked)
{
    if(checked) {
        ui->RFID_Btn->setText("关闭读取标签");
        rfidThread->start();
        ui->RFIDNum_Label->setStyleSheet(qlabel_open);
        ui->EPCTime_Label->setStyleSheet(qlabel_open);
        ui->instantEPC_Label->setStyleSheet(qlabel_open);
        ui->actualEPCTime_Label->setStyleSheet(actualQLabel_open);
        ui->actualInstantEPC_Label->setStyleSheet(actualQLabel_open);
        ui->actualRFIDNum_Label->setStyleSheet(actualQLabel_open);

    } else {
        rfidThread->stop();
        ui->RFID_Btn->setText(" 开始读取标签 ");
        ui->RFIDNum_Label->setStyleSheet(qlabel_close);
        ui->EPCTime_Label->setStyleSheet(qlabel_close);
        ui->instantEPC_Label->setStyleSheet(qlabel_close);
        ui->actualEPCTime_Label->setStyleSheet(actualQLabel_close);
        ui->actualInstantEPC_Label->setStyleSheet(actualQLabel_close);
        ui->actualRFIDNum_Label->setStyleSheet(actualQLabel_close);
    }
}
